package com.crazymaker.springcloud.cloud.center.zuul.filter;


import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Component
public class DemoFilter extends ZuulFilter
{

    @Override
    public String filterType()
    {
//		pre：路由之前
//		routing：路由之时
//		post： 路由之后
//		error：发送错误调用
        return "pre";
    }

    /**
     * 过滤的顺序
     */
    @Override
    public int filterOrder()
    {
        return 0;
    }

    /**
     * 这里可以写逻辑判断，是否要过滤，true为永远过滤。
     */
    @Override
    public boolean shouldFilter()
    {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();

        if (request.getRequestURI().startsWith("/demo" ))
        {
            return true;
        }

        return false;
    }

    /**
     * 过滤器的具体逻辑
     */
    @Override
    public Object run()
    {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();

/**
 * 添加header demo
 *
 */
//        ctx.addZuulRequestHeader("newHeader", "111111");

        Object username = request.getParameter("username" );
        if (username != null && "filterTest".equals(username.toString()))
        {
            log.info(request.getMethod() + "--" + request.getRequestURL().toString());
            ctx.setSendZuulResponse(false);
            try
            {
                ctx.getResponse().setContentType("text/html;charset=utf-8" );
                ctx.getResponse().getWriter().write("监控到用户名是fymod" );
            } catch (Exception e)
            {
                e.printStackTrace();
            }
            return null;
        }
        return null;
    }

}